package com.k.demo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.mapping.FetchType;

import com.k.demo.entity.Evaluation;

@Mapper
public interface EvaluationMapper {
	
	@Update("update evaluation set is_delete = 1 where evaluation_id=#{evaluation_id}") 
	int delete(int evaluation_id);
	
	
//	@Results(id = "EvaluationWithUser",value={
//			@Result(id = true,column = "evaluation_id",property = "evaluation_id"),
//			@Result(column = "user_id",property = "user_id"),
//			@Result(column = "food_id",property = "food_id"),
//			@Result(column = "evaluation_time",property = "evaluation_time"),
//			@Result(column = "evaluation_content",property = "evaluation_content"),
//			@Result(column = "evaluation_img",property = "evaluation_img"),
//			@Result(column = "evaluation_lv",property = "evaluation_lv"),
//			@Result(column = "is_delete",property = "is_delete"),
//			@Result(column = "deptno",property = "customeruser",
//			one = @One(select="com.k.demo.mapper.CustomerUserMapper.selectCustomerUserByUserid",fetchType = FetchType.EAGER)),
//	})
	//@Select("select * from evaluation where food_id = #{food_id}")
	List<Evaluation> selectAll(int food_id);
	
	@ResultMap("EvaluationWithUser")
	@Select("select * from evaluation where food_id=#{food_id} and evaluation_img is not null order by evaluation_time desc")
	List<Evaluation> selectWithImg(int food_id);
	
	
	@ResultMap("EvaluationWithUser")
	@Select("select * from evaluation where food_id=#{food_id} and evaluation_lv>=4 order by evaluation_time desc")
	List<Evaluation> selectQuality(int food_id);
	
	
	@Insert("insert into evaluation (evaluation_id,user_id,food_id,evaluation_time,evaluation_content,evaluation_img,evaluation_lv,is_delete)"
			+ "values (EVALUATION_SEQ.NEXTVAL,#{user_id},#{food_id},sysdate,#{evaluation_content},#{evaluation_img},#{evaluation_lv},1)")
	int insert(Evaluation evaluation);
	
	@Select("select nvl(avg(evaluation_lv),5) from evaluation where food_id = #{food_id} and is_delete = 1")
	Double selectavglv(int food_id);
	
	@Select("select count(evaluation_id) from evaluation where food_id = #{food_id} and is_delete = 1")
	int count(int food_id);
}
